<?php


namespace app\partner\controller;

use redis\RedisPackage;
use think\Controller;

class Login extends Controller
{
    public function userLogin()
    {
        $input = input();
        if(empty($input['tel']) || empty($input['password'])) {
            returnError('电话或密码不能为空');
        }
        $user = db('user_manage')
            ->where('tel', $input['tel'])
            ->find();
        if(empty($user)) {
            returnError('用户不存在');
        }
        //判断当前用户是否是合伙人
        $partner = db('super_partner')
            ->where('uid', $user['id'])
            ->find();
        if(empty($partner)) {
            returnError('该合伙人不存在');
        }
        if($partner['password'] != md5($input['password'])) {
            returnError('密码不正确');
        }
        switch ($partner['status']){
            case 0:
                returnError('账号已被禁用！');
                break;
            case 2:
                returnError('请先进行账号绑定！');
                break;
            case 3:
                returnError('账号审核中，请耐心等待！');
                break;
        }


        $ip = get_real_ip();
        $area = get_login_area($ip);
        $browser = browse_info();
        $data['uid'] = $partner['id'];
        $data['login_time'] = time();
        $data['login_ip'] = $ip;
        $data['login_area'] = $area;
        $data['login_browser'] = $browser;
        $data['user_type'] = 1;
        db('login_log')
            ->insertGetId($data);
        $token = md5($partner['id'] . time() .mt_rand(1,999999)). $partner['id'];
        $key = 'uid_'.$partner['id'];
        RedisPackage::getInstance();
        RedisPackage::set($key, $token, 86400);
        returnSuccess(['token'=>$token,'username'=>$user['nickname']], '登录成功');
    }

    /**
     * 忘记密码下一步
     */
    public function nextStep()
    {
        RedisPackage::getInstance();
        $input = input();
        $tel = isset($input['tel']) ? $input['tel'] : '';
        $smscode = isset($input['smscode']) ? $input['smscode'] : '';
        if(empty($tel) || empty($smscode)) {
            returnError('缺少重要参数');
        }
        //验证手机号
        $user = db('super_partner')
            ->alias('p')
            ->join('user_manage u', 'p.uid=u.id', 'LEFT')
            ->where('u.tel', $tel)
            ->field('p.id')
            ->find();
        if(empty($user)) {
            returnError('该合伙人手机不存在');
        }
        $code = RedisPackage::get('code_' . $tel);
        if(empty($code)) {
            returnError('验证码已过期请重新发送');
        }
        if($code != $smscode) {
            returnError('验证码错误请重新输入');
        }
        RedisPackage::del('code_' . $tel);
        returnSuccess(['id'=>$user['id']], '验证成功');
    }

    /**
     * 修改密码
     */
    public function forgetPassword()
    {
        $input = input();
        $id = isset($input['id']) ? $input['id'] : 0;
        $password = isset($input['password']) ? $input['password'] : '';
        if(empty($id) || empty($password)) {
            returnError('缺少重要参数');
        }
        $partner = db('super_partner')
            ->where('id', $id)
            ->find();
        if(empty($partner)) {
            returnError('该合伙人不存在');
        }
        $result = db('super_partner')
            ->where('id', $partner['id'])
            ->update(['password'=>md5($password)]);
        if($result) {
            returnSuccess([], '密码修改成功');
        } else {
            returnError('密码修改失败');
        }
    }
}